/**
 * 
 * purpose: store loader callback method...
 * @auth: Ning
 * date: 2009-8-25
 * 
 * */
package com.components.loader
{
	import core.error.ErrorEvents;
	import core.error.Errors;
	
	import flash.events.Event;
	import flash.events.ProgressEvent;
	
	public class LoaderCallback
	{
		//START...
		
		//PROPERTY...
		
		private var _success:Array;
		
		private var _fail:Array;
		
		private var _progress:Array;
		
		private var _target:I_Loader;
		
		public static const STATE_SUCCESS:String = "_success";
		
		public static const STATE_FAIL:String = "_fail";
		
		public static const STATE_PROGRESS:String = "_progress";
		
		//CONSTRUCTOR...
		public function LoaderCallback(success:Array,fail:Array = null,progress:Array = null)
		{
			
			_success = success;
			
			_fail = !fail ? [] : fail;
			
			_progress = !progress ? [] : progress;	
			
		}
		
		//METHODS...
		
		/**
		 * 
		 * private methods start...
		 * 
		 * */
		
		//config target event listeners...
		private function configEventListeners():void
		{
			
			target.addEventListener(Event.COMPLETE,targetLoadComplete);
			
			target.addEventListener(ErrorEvents.IO_ERROR,targetFailHandler);
			
			target.addEventListener(ErrorEvents.SECURITY_ERROR,targetFailHandler);
			
			target.addEventListener(ErrorEvents.SYNTAX_ERROR,targetFailHandler);
			
			target.addEventListener(ProgressEvent.PROGRESS,targetProgressHandler);
			
		}
		
		//target load complete handler....
		private function targetLoadComplete(evt:Event):void
		{
			
			var content:* = target.content;
									
			var callback:Function;
						
			for(var i:int = _success.length - 1; i >= 0;i--)
			{
				
				callback = _success[i] as Function;
				
				if(!(callback is Function)) continue;
				
				callback.call(null,content);				
				
			}  
			
		}
		
		//target load ioerror handler....
		private function targetFailHandler(evt:Event):void
		{
			
			var callback:Function;
			
			var error:Errors = new Errors(evt.type + ":" + target.url);
			
			var errEvt:ErrorEvents = new ErrorEvents(ErrorEvents.IO_ERROR,error);
						
			for(var i:int = _fail.length - 1;_fail.length > 0 && i >= 0;i--)
			{
				
				callback = _fail[i] as Function;
				
				if(!(callback is Function)) continue;
								
				callback.call(null,errEvt);				
				
			}  
			
		}		
		
		//target load progress handler...
		private function targetProgressHandler(evt:ProgressEvent):void
		{
			
			var callback:Function;
												
			for(var i:int = _progress.length - 1;_progress.length > 0 && i >= 0;i--)
			{
				
				callback = _progress[i] as Function;
				
				if(!(callback is Function)) continue;
				
				callback.call(null,evt);				
				
			}  
			
		}
		
		
		/**
		 * 
		 * public methods start...
		 * 
		 * */
		
		//add success callback...
		public function addSuccessCallback(callback:Function,pri:uint = 0):void
		{
			
			_success.splice(pri,0,callback);
		
		}
		
		//add fail callback....
		public function addFailCallback(callback:Function,pri:uint = 0):void
		{
			
			_fail.splice(pri,0,callback);
			
		}
		
		//add progress callback....
		public function addProgressCallback(callback:Function,pri:uint = 0):void
		{
			
			_progress.splice(pri,0,callback);
			
		}
		
		//remove callback methods...
		public function removeCallBack(callback:Function,state:String):void
		{
			
			var callbackCollection:Array = this[state];
			
			for(var i:uint = 0 ;i < callbackCollection.length;i++)
			{
				
				if(callbackCollection[i] === callback)
				{
					
					callbackCollection.splice(i,1);
					
					i--;
										
				}
				
			}			
			
		}
		
		//remove all callback method...
		public function removeAllCallbacks():void
		{
			
			_fail = [];
			
			_progress = [];
			
			_success = [];
			
		}
		
		//set target...
		public function set target(_target:I_Loader):void
		{
			
			this._target = _target;
			
			configEventListeners();
			
		}
		
		//get target...
		public function get target():I_Loader
		{
			
			return _target;
			
		}
		
		//END
	}
}